package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.Section;
import com.zygh.policeasset.service.SectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("招标标段")
@RequestMapping({"/section"})
@RestController
public class SectionController {
    @Autowired
    SectionService sectionService;

    @RequestMapping(value = {"/findall"}, method = {RequestMethod.GET})
    @ApiOperation("查询某项目、招标、状态下所有标段")
    public Object findAll(@RequestBody Section section) {
        return this.sectionService.findAll(section);
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加标段")
    public Object add(@RequestBody Section section) {
        if (StrUtil.isEmpty(section.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (StrUtil.isEmpty(section.getProjectName())) {
            return new CommonResult().validateFailed("项目名称不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(section.getTenderId()))) {
            return new CommonResult().validateFailed("招标id不能为空");
        }
        int count = this.sectionService.add(section);
        if (count > 0) {
            return new CommonResult().success(Integer.valueOf(count));
        }
        return new CommonResult().validateFailed("更新失败");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改标段")
    public Object update(@RequestBody Section section) {
        if (StrUtil.isEmpty(section.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(section.getTenderId()))) {
            return new CommonResult().validateFailed("招标id不能为空");
        }
        if (this.sectionService.update(section) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("更新失败");
    }

    @RequestMapping(value = {"/remove"}, method = {RequestMethod.GET})
    @ApiOperation("删除标段")
    public Object remove(@RequestParam int id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.sectionService.delete(Integer.valueOf(id)) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @RequestMapping(value = {"/getaddassect"}, method = {RequestMethod.GET})
    @ApiOperation("获得可添加但未添加资产的标段")
    public Object getAddAssect(@RequestBody Section section) {
        return this.sectionService.getAddAssect(section);
    }


    @RequestMapping(value = {"/selectProjectNo"}, method = {RequestMethod.GET})
    @ApiOperation("根据项目名称查询标段")
    public Object selectProjectNo(@RequestParam String projectNo) {
        return new CommonResult().success(this.sectionService.selectProjectNo(projectNo));
    }

}